package com.jkgl.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jkgl.common.utils.StringUtils;
import com.jkgl.entity.UserInfoSelfEntity;
import com.jkgl.mapper.UserInfoSelfMapper;
import com.jkgl.model.result.SelfUserVO;
import com.jkgl.service.UserInfoSelfService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author w020304m
 * @since 2025-02-11
 */
@Service
@RequiredArgsConstructor
public class UserInfoSelfServiceImpl extends ServiceImpl<UserInfoSelfMapper, UserInfoSelfEntity> implements UserInfoSelfService {

    private final Snowflake snowflake;

    @Override
    public UserInfoSelfEntity getInfoSelf(Long userId) {
        QueryWrapper<UserInfoSelfEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UserInfoSelfEntity::getUserId,userId);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public void create(UserInfoSelfEntity infoSelf) {
        QueryWrapper<UserInfoSelfEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UserInfoSelfEntity::getUserId,infoSelf.getUserId());
        UserInfoSelfEntity entity = baseMapper.selectOne(queryWrapper);

        if(entity == null){
            if(StringUtils.isNotEmpty(infoSelf.getWeight()) && StringUtils.isNotEmpty(infoSelf.getHeight())){
                Double height = Double.parseDouble(infoSelf.getHeight())/100;
                double weight = Double.parseDouble(infoSelf.getWeight());
                infoSelf.setBmi(Double.toString(weight/(height*height)));
            }
            infoSelf.setUserId(infoSelf.getUserId());
            infoSelf.setId(snowflake.nextIdStr());
            infoSelf.setCreateTime(LocalDateTime.now());
            infoSelf.setMaxDays(0);
            baseMapper.insert(infoSelf);
        }else{
            if(StringUtils.isNotEmpty(infoSelf.getWeight()) && StringUtils.isNotEmpty(infoSelf.getHeight())){
                Double height = Double.parseDouble(infoSelf.getHeight())/100;
                double weight = Double.parseDouble(infoSelf.getWeight());
                entity.setBmi(Double.toString(weight/(height*height)));
            }
            entity.setAge(infoSelf.getAge());
            entity.setHeight(infoSelf.getHeight());
            entity.setMedicalHistory(infoSelf.getMedicalHistory());
            entity.setTargetStep(infoSelf.getTargetStep());
            entity.setWeight(infoSelf.getWeight());
            entity.setTargetWeight(infoSelf.getTargetWeight());
            entity.setIsSmoke(infoSelf.getIsSmoke());
            entity.setUpdateTime(LocalDateTime.now());
            baseMapper.updateById(entity);
        }
    }

    @Override
    public List<SelfUserVO> getBMIAll(Integer minAge, Integer maxAge) {
        return baseMapper.getBMIAll(minAge,maxAge);
    }
}
